<template>
  <view>
    <view class="container form-content">
      <uni-forms ref="form" :modelValue="formData" :rules="rules" labelWidth="120px" labelPosition="top">
        <view class="container">
          <view class="hidden-title">基本信息</view>
          <view class="background-white">
            <view class="flex text bor-btm"
              >维护保养项目：
              <text>{{ detail.item }}</text>
            </view>
            <view class="flex text bor-btm"
              >维护保养内容：
              <text>{{ detail.content }}</text>
            </view>
            <view class="flex text bor-btm"
              >维护保养标准：
              <text>{{ detail.standard }}</text>
            </view>
          </view>
        </view>
        <view>
          <view class="hidden-title">维护结果</view>
          <view class="background-white">
            <uni-forms-item name="record" label="维护保养记录">
              <textarea
                v-model="formData.record"
                class="text-area"
                placeholder="简要填写(限1000字)"
                :disabled="readOnly"
                maxlength="1000"
              />
            </uni-forms-item>
          </view>
          <view class="background-white">
            <uni-forms-item name="participantPerson" label="维护保养参与人">
              <textarea
                v-model="formData.participantPerson"
                class="text-area"
                placeholder="简要填写(限1000字)"
                :disabled="readOnly"
                maxlength="1000"
              />
            </uni-forms-item>
          </view>
        </view>
      </uni-forms>
    </view>
    <view class="submit-group">
      <button class="u-button block" :ripple="true" @click="handleClickStage(1)">正常</button>
      <button class="u-button u-button-orange block" :ripple="true" @click="handleClickStage(3)">上报缺陷</button>
    </view>
  </view>
</template>

<script>
import { maintenanceExecute } from '../../../../api/equipment/maintenance'

export default {
  data() {
    return {
      areaId: '',
      location: '',
      btnName: '',
      taskId: '',
      readOnly: false,
      deviceId: '',
      deviceName: '',
      formData: {
        id: '',
        record: '',
        participantPerson: '',
        resultFlag: ''
      },
      detail: {
        item: '',
        deviceId: '',
        content: '',
        standard: ''
      },
      rules: {
        record: {
          rules: [
            {
              required: true,
              errorMessage: '维护保养记录不能为空'
            }
          ]
        },
        participantPerson: {
          rules: [
            {
              required: true,
              errorMessage: '维护保养参与人不能为空'
            }
          ]
        }
      }
    }
  },
  onLoad({ taskId, readOnly, location, info, operate, deviceId, deviceName, areaId }) {
    this.location = location
    this.btnName = operate
    this.readOnly = readOnly
    this.deviceId = deviceId
    this.deviceName = deviceName
    this.areaId = areaId
    this.formData.id = this.taskId = taskId
    Object.assign(this.detail, JSON.parse(info))
    this.setTitle(this.detail.item)
  },
  onReady() {
    this.$refs.form.setRules(this.rules)
  },
  onPullDownRefresh() {},
  methods: {
    setTitle(title) {
      uni.setNavigationBarTitle({
        title: title
      })
    },
    navigateBack() {
      uni.navigateBack()
    },
    goMatterReport() {
      uni.navigateTo({
        url: '/pages/work/intelligentPatrolInspection/maintenanceMatter/matterReport?deviceId=' + this.detail.deviceId
      })
    },

    handleClickStage(type) {
      this.$refs.form.validate().then((res) => {
        if (type === 1) {
          //直接保存
          this.formData.resultFlag = '0'
          this.saveForm(this.formData)
        } else if (type === 2) {
          this.formData.resultStatus = '2'
          uni.navigateTo({
            url: `/pages/work/hiddenDangerReport/index?reportType=4&areaId=${this.areaId}`,
            // url: '/pages/work/intelligentPatrolInspection/maintenanceMatter/matterReport?deviceId=' +
            // 	this.detail.deviceId,
            events: {
              'callback:success': (data) => {
                this.saveForm(this.formData)
              }
            }
          })
        } else {
          this.formData.resultFlag = '1'
          uni.navigateTo({
            url: `/pages/work/intelligentPatrolInspection/maintenanceMatter/matterReportDefects?location=${this.location}&deviceId=${this.deviceId}&deviceName=${this.deviceName}`,
            events: {
              'callback:success': (data) => {
                this.saveForm(this.formData)
              }
            }
          })
        }
      })
    },
    saveForm(data) {
      console.log('data', data)
      maintenanceExecute(data).then(() => {
        this.$modal.msgSuccess('执行成功')
        this.navigateBack()
      })
    }
  }
}
</script>

<style lang="scss">
.container {
  .background-white {
    background-color: #fff;
    padding: 20rpx 30rpx;
    box-sizing: border-box;

    .uni-forms-item__label {
      color: #333;
      font-size: 30rpx;
    }

    .bor-btm {
      display: flex;
      border-bottom: 1px solid #d5d7e3;
    }

    .text {
      padding-bottom: 20rpx;
      padding-top: 4rpx;
      color: #333;

      text {
        color: #666;
      }
    }
  }
}

.hidden-title {
  background: #f5f5f5;
  padding: 20rpx;
  color: #999;
}

.bor-top {
  border-top: 20rpx solid #f5f5f5;
}

.form-content {
  margin-bottom: 120px;
}
</style>
